package core;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;

import core.units.Unit;

public class DirectionChoice {
	int x, y;
	int lineRange = 80;
	double rotationSpeed = 0.4; // keep rotationSpeed > 0
	double optionPlace=0;
	double optionMin = 0, optionMax = 0;
	boolean clockwise = true;
	String followingAction;//old
	boolean followUnit=false;
	Unit unit;
	
	
	//Old
	public DirectionChoice(int inX, int inY,String inFollowingAction){
		x=inX;
		y=inY;
		followingAction = inFollowingAction;
	}
	
	public DirectionChoice(int inX, int inY){
		x=inX;
		y=inY;
		//System.out.println("i dirChoice-konst");
	}
	public DirectionChoice(Point p){
		this(p.x, p.y);
	}
	public DirectionChoice(Unit u){
		this(u.getPoint());
		unit=u;
		followUnit=true;
	}
	
	
	public void nextFrame(){
		double oldDir = optionPlace;
		if(clockwise){
			optionPlace=optionPlace + rotationSpeed;
			if(oldDir <= optionMax && optionPlace >= optionMax)
				clockwise = false;
			if(optionPlace>=100)
				optionPlace -= 100;
		}
		else{
			optionPlace=optionPlace - rotationSpeed;
			if(oldDir >= optionMin && optionPlace <= optionMin)
				clockwise = true;
			if(optionPlace<=0)
				optionPlace += 100;
		}	
		
		if(followUnit){
			x=unit.getX();
			y=unit.getY();
		}
	}
	
	/*public void paint(Graphics g){
		g.setColor(Color.white);
		g.drawOval(x-20, y-20, 40, 40);
		g.drawLine(x, y, (int)(x+Math.cos(optionPlace*3.1415*2/100)*50/2), (int)(y+Math.sin(optionPlace*3.1415*2/100)*50/2));
	}*/
	public void paint(Point screenOffset, int zoomDivider, Graphics g){
		g.setColor(Color.white);
		if(optionPlace >= 100)
			g.setColor(Color.red);
		if(optionPlace <= 0)
			g.setColor(Color.blue);
		int drawX = (x-screenOffset.x)/zoomDivider;
		int drawY = (y-screenOffset.y)/zoomDivider;
		g.drawOval(drawX-20, drawY-20, 40, 40);
		g.drawLine(drawX, drawY, (int)(drawX+Math.cos(optionPlace*3.1415*2/100)*lineRange/2), (int)(drawY+Math.sin(optionPlace*3.1415*2/100)*lineRange/2));
	}
	public double getDir(){
		return optionPlace/100;
	}
	public int getX(){
		return x;
	}
	public int getY(){
		return y;
	}

	public void setX(int inX) {
		x = inX;
	}
	public void setY(int inY) {
		y = inY;
	}
	/**
	 * 
	 * @param center 0-1, 0 pointing right
	 * @param arcLength 0-1
	 */
	public void setArc(double center, double arcLength){
		optionMin=(center-arcLength/2+2)*100%100;
		System.out.println("optionMin is "+optionMin);
		optionMax=(center+arcLength/2+2)*100%100;
		optionPlace = optionMin;
	}
	public void setSpeed(double speed){
		rotationSpeed = speed;
	}
	public void setLineRange(int range){
		lineRange = range;
	}
	
}
